/**
 * Add an onUpdate listener to tabs immediately after their creation 
 */
chrome.tabs.onCreated.addListener(function(tab) {
	chrome.tabs.onUpdated.addListener(sentRequest);
});

/**
 * Add an onUpdate listener to all current open tabs
 */
chrome.tabs.onUpdated.addListener(sentRequest);

/**
 * Send request to the content script to start testing of the page
 */
function sentRequest(tabId, changeInfo, tab) {
	chrome.tabs.sendRequest(tab.id, {action: "testPage"});
};

/**
 * 
 */
chrome.tabs.onRemoved.addListener(function(tabId, removeInfo) {
	localStorage.removeItem(tabId);
});

/**
 * 
 */
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
	var tabId = sender.tab.id;
	
	if (request.setLibs) {
		/*
		 * Set into the background page's localStorage
		 * if the passed value is valid object
		 */
		if (JSON.parse(request.setLibs).constructor === Object) {
			//keep the value into the localStorage
			localStorage[tabId] = request.setLibs;
			
			//Show icon
			chrome.pageAction.show(tabId);
		}
		
		sendResponse({});
	}
});